package gui.properties;

import java.awt.Color;
import java.awt.Component;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.LinkedList;
import java.util.List;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import model.MetroModel;
import model.objects.EdiLine;

import com.nightpiter.model.objects.Line;

public class LinePropertiesPanel extends FlowPanel{
	private EdiLine line;
	
	public LinePropertiesPanel(EdiLine line) {
		super(line.getName());
		this.line = line;
		this.setPanelComponents(this.initPanelComponents());
		this.setPrVisible(true);
	}
	private List<Component> initPanelComponents() {
		List<Component> comps = new LinkedList<Component>();	
		
		JPanel namePanel = new JPanel();
		namePanel.setLayout(new GridLayout(0,2));
		namePanel.add(new JLabel("Name:"));
		namePanel.add(new NameField());
		comps.add(namePanel);
		
		JPanel delayPanel = new JPanel();
		delayPanel.setLayout(new GridLayout(0,2));
		delayPanel.add(new JLabel("Train delay:"));
		delayPanel.add(new DelayField());
		comps.add(delayPanel);
		
		JPanel colorPanel = new JPanel();
		colorPanel.setLayout(new GridLayout(0,2));
		JPanel colorSecondaryPanel = new JPanel();
		colorPanel.add(new JLabel("Color:"));
		colorSecondaryPanel.setLayout(new BoxLayout(colorSecondaryPanel, BoxLayout.Y_AXIS));
		colorSecondaryPanel.add(new ColorField());
		colorSecondaryPanel.add(new PickColorButton());
		colorPanel.add(colorSecondaryPanel);
		comps.add(colorPanel);
		
		return comps;
	}
	
	class NameField extends JTextField {
		public NameField() {
			//final JTextField nameField = this;
			setText(line.getName());		
			
			addKeyListener(new KeyListener() {

				@Override
				public void keyPressed(KeyEvent arg0) {
					if(arg0.getKeyCode()==KeyEvent.VK_ENTER) {
						model.MetroModel.getInstance().setLineName(line, getText());
					}				
				}

				@Override
				public void keyReleased(KeyEvent arg0) {
					
					
				}

				@Override
				public void keyTyped(KeyEvent arg0) {
					
				}
				
			});
		}
		
	}
	
	class DelayField extends JTextField {
		public DelayField() {
			//final JTextField nameField = this;
			setText(line.getTrainDelay()+"");		
			
			addKeyListener(new KeyListener() {

				@Override
				public void keyPressed(KeyEvent arg0) {
					if(arg0.getKeyCode()==KeyEvent.VK_ENTER) {
						model.MetroModel.getInstance().setLineDelay(line, Integer.parseInt(getText()));
					}				
				}

				@Override
				public void keyReleased(KeyEvent arg0) {
					
					
				}

				@Override
				public void keyTyped(KeyEvent arg0) {
					
				}
				
			});
		}
		
	}
	
	class ColorField extends JTextField {
		public ColorField() {
			//final JTextField nameField = this;
			setText(colorToHex(new Color(line.getColor())));		
			
			addKeyListener(new KeyListener() {

				@Override
				public void keyPressed(KeyEvent arg0) {
					if(arg0.getKeyCode()==KeyEvent.VK_ENTER) {
						model.MetroModel.getInstance().setLineColor(line, Color.decode(getText()));
					}				
				}

				@Override
				public void keyReleased(KeyEvent arg0) {
					
					
				}

				@Override
				public void keyTyped(KeyEvent arg0) {
					
				}
				
			});
		}
		
		private String colorToHex(Color color) {
			String c = "#";
			
			c+=numberToLetter(color.getRed()/16);
			c+=numberToLetter(color.getRed()%16);
			c+=numberToLetter(color.getGreen()/16);
			c+=numberToLetter(color.getGreen()%16);
			c+=numberToLetter(color.getBlue()/16);
			c+=numberToLetter(color.getBlue()%16);
			return c;
		}
		
		private char numberToLetter(int num) {
			
			switch(num) {
			case 0:
				return '0';
			case 1:
				return '1';
			case 2:
				return '2';
			case 3:
				return '3';
			case 4:
				return '4';
			case 5:
				return '5';
			case 6:
				return '6';
			case 7:
				return '7';
			case 8:
				return '8';
			case 9:
				return '9';
			case 10:
				return 'A';
			case 11:
				return 'B';
			case 12:
				return 'C';
			case 13:
				return 'D';
			case 14:
				return 'E';
			case 15:
				return 'F';
			}
			return '-';
		}
		
	}
	
	class PickColorButton extends JButton {
		public PickColorButton() {
			super("pick color");
			addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(ActionEvent arg0) {
					final JColorChooser colorChooser = new JColorChooser(new Color(line.getColor()));
					ActionListener onOk = new ActionListener() {

						@Override
						public void actionPerformed(ActionEvent e) {
							MetroModel.getInstance().setLineColor(line, colorChooser.getColor());
							
						}
						
					};
					ActionListener onCancel = new ActionListener() {

						@Override
						public void actionPerformed(ActionEvent e) {							
							//do nothing
						}
						
					};
					
					JColorChooser.createDialog(prPanel, "Choose line color", true, colorChooser, onOk, onCancel).setVisible(true);
					

				}
				
			});
			//final JTextField nameField = this;
			//setText(line.getColor()+"");		
		}		
	}

	
}
